import sqlite3
import json
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import os
import shutil
import sys
import page2
import page4
import change_class
from tools.MyWidget import ClickLabel


class ClassManager(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.setGeometry(350,250,754,460)
        self.layout = QVBoxLayout()
        self.setLayout(self.layout)
        self.main_widget = QWidget()
        self.main_layout = QHBoxLayout()
        self.main_widget.setLayout(self.main_layout)
        self.left_widget = LeftWidget()
        self.main_layout.addWidget(self.left_widget)
        self.right_widget = RightWidget()
        self.main_layout.addWidget(self.right_widget)
        self.top = TopWidget()
        self.layout.addWidget(self.top)
        self.layout.addWidget(self.main_widget)
class TopWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        #设置左侧的内容
        self.left_pic_widget = QWidget()
        self.logo = QLabel(self.left_pic_widget)
        self.pix = QPixmap("static/cqu.jpg")
        self.pix = self.pix.scaled(39, 39)
        self.logo.setPixmap(QPixmap(self.pix))
        self.tittle = QLabel(self.left_pic_widget)
        self.tittle.setText("学生成绩管理系统")
        #设置右侧的内容
        self.right_pic_widget = QWidget()
        self.right_pic_widget.setFixedWidth(120)
        self.side = QLabel(self.right_pic_widget)
        self.side.setFixedSize(60,35)
        self.pic = QPixmap("static/people.jpg")
        self.pic = self.pic.scaled(42, 33)
        self.side.setPixmap(QPixmap(self.pic))
        self.welcom_lab = QLabel(self.right_pic_widget)
        self.side_label = ClickLabel(self.right_pic_widget)
        self.side_label.setText("管理")
        self.setFixedHeight(57)
        self.init_ui()
        self.set_widget_style()
        self.init_dynamic()
    def manage(self):
        self.w2 = page4.Test()
        self.w2.show()
    def init_dynamic(self):
        with open("tmp/user") as f:
            line = f.read()
            line = line.split(",")
        sql = "select * from user_"+line[0]+" where username='"+line[1].rstrip()+"'"
        conn = sqlite3.connect("class_manager.db")
        cursor = conn.cursor()
        result = cursor.execute(sql)
        result = result.fetchall()[0]
        self.welcom_lab.setText("你好,"+result[2])
    def init_ui(self):
        #设置背景色
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.logo.move(10,0)
        self.tittle.move(50,10)
        self.side.move(70,5)
        self.welcom_lab.move(0,15)
        self.side_label.move(80,40)
        self.side_label.connect_customized_slot(self.manage)
    def set_widget_style(self):
        self.layout = QHBoxLayout()
        self.layout.setContentsMargins(0,0,0,0)
        self.setLayout(self.layout)
        self.layout.addWidget(self.left_pic_widget)
        self.layout.addWidget(self.right_pic_widget)
        font = QFont('PingFang SC Regular',20)
        font.setPixelSize(20)
        self.tittle.setFont(font)

class LeftWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.setFixedSize(530,325)
        self.class_lab = QLabel(self)
        self.pix = QPixmap("static/class.jpg")
        self.pix = self.pix.scaled(42, 32, Qt.KeepAspectRatio)
        self.class_lab.setPixmap(QPixmap(self.pix))
        self.class_lab2 = QLabel(self)
        self.class_lab2.setPixmap(QPixmap(self.pix))
        self.class_lab3 = QLabel(self)
        self.class_lab3.setPixmap(QPixmap(self.pix))
        self.cls_lab1 = QLabel("基础课",self)
        self.cls_lab2 = QLabel("核心课",self)
        self.cls_lab3 = QLabel("专业课",self)
        self.init_ui()
        self.set_widget_style()
        self.init_dynamic()
    def init_ui(self):
        pal = QPalette(self.palette())
        pal.setColor(QPalette.Background,QColor(255,255,255))
        self.setAutoFillBackground(True)
        self.setPalette(pal)
        self.class_lab.move(10,20)
        self.class_lab2.move(10,120)
        self.class_lab3.move(10,220)
        self.cls_lab1.move(60,20)
        self.cls_lab2.move(60,120)
        self.cls_lab3.move(60,220)
    def init_dynamic(self):
        with open("tmp/user") as f:
            line = f.read()
            line = line.split(",")
        sql = "select * from user_"+line[0]+" where username='"+line[1].rstrip()+"'"
        conn = sqlite3.connect("class_manager.db")
        cursor = conn.cursor()
        result = cursor.execute(sql)
        result = result.fetchall()[0]
        if not os.path.exists("dynamic/"+result[0]):
            os.mkdir("dynamic/"+result[0])
            os.mkdir("dynamic/"+result[0]+"/基础课")
            os.mkdir("dynamic/"+result[0]+"/核心课")
            os.mkdir("dynamic/"+result[0]+"/专业课")
            with open("tmp/class.json",'w') as f:
                class_dict = {"classname":""}
                f.write(json.dumps(class_dict))
            shutil.copyfile("tmp/class.json","dynamic/"+result[0]+"/基础课/class.json")
            shutil.copyfile("tmp/class.json","dynamic/"+result[0]+"/核心课/class.json")
            shutil.copyfile("tmp/class.json","dynamic/"+result[0]+"/专业课/class.json")
            os.remove("tmp/class.json")
        else:
            with open("dynamic/"+result[0]+"/基础课/class.json") as f:
                base_class = json.loads(f.read())
            with open("dynamic/"+result[0]+"/核心课/class.json") as f:
                core_class = json.loads(f.read())
            with open("dynamic/"+result[0]+"/专业课/class.json") as f:
                major_class = json.loads(f.read())
            font = QFont("PingFang SC Regular",16)
            font.setPixelSize(16)
            for i in range(len(base_class["classname"])):
                lab = QLabel(self)
                lab.setText(base_class["classname"][i])
                lab.setFont(font)
                lab.move(60,25+(i+1)*25)
            for i in range(len(core_class["classname"])):
                lab = QLabel(self)
                lab.setText(core_class["classname"][i])
                lab.setFont(font)
                lab.move(60,125+(i+1)*25)
            for i in range(len(major_class["classname"])):
                lab = QLabel(self)
                lab.setText(major_class["classname"][i])
                lab.setFont(font)
                lab.move(60,225+(i+1)*25)
    def set_widget_style(self):
        font = QFont("PingFang SC Regular",20)
        font.setPixelSize(20)
        self.cls_lab1.setFont(font)
        self.cls_lab2.setFont(font)
        self.cls_lab3.setFont(font)
class RightWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.resize(110,325)
        self.btn1 = QPushButton("新建课程",self) 
        self.btn2 = QPushButton("修改课程",self)
        self.btn3 = QPushButton("汇总统计",self)
        self.btn4 = QPushButton("毕业指标",self)
        self.init_ui()
        self.set_widget_style()
    def init_ui(self):
        self.btn1.clicked.connect(self.createClass)
        self.btn2.clicked.connect(self.updateClass)
        self.btn1.move(7,10)
        self.btn2.move(7,95)
        self.btn3.move(7,180)
        self.btn4.move(7,265)
    def set_widget_style(self):
        self.btn1.setFixedSize(155,65)
        self.btn2.setFixedSize(155,65)
        self.btn3.setFixedSize(155,65)
        self.btn4.setFixedSize(155,65)
        font = QFont("PingFang SC Regular")
        font.setPixelSize(18)
        self.btn1.setFont(font)
        self.btn2.setFont(font)
        self.btn3.setFont(font)
        self.btn4.setFont(font)
        self.btn1.setStyleSheet("background-color: rgb(255, 255, 255);border:none;")
        self.btn2.setStyleSheet("background-color: rgb(255, 255, 255);border:none;")
        self.btn3.setStyleSheet("background-color: rgb(255, 255, 255);border:none;")
        self.btn4.setStyleSheet("background-color: rgb(255, 255, 255);border:none;")
    def createClass(self):
        self.window = page2.ClassManager()
        self.window.show()
    def updateClass(self):
        self.window = change_class.ClassManager()
        self.window.show()

if __name__=="__main__":
    app = QApplication(sys.argv)
    window = ClassManager()
    window.show()
    sys.exit(app.exec_())